<?php

/**
 * Shopalong language class
 *
 * $Id: language.php,v 1.16 2003/06/05 23:16:46 hpdl Exp $
 * Copyright (c) 2003 osCommerce
 * browser language detection logic Copyright phpMyAdmin (select_lang.lib.php3 v1.24 04/19/2002)
 * Copyright Stephane Garin <sgarin@sgarin.com> (detect_language.php v0.1 04/02/2002)
 * Portions Copyright (c) 2006 Shopalong
 *
 * Released under the GNU General Public License
 */

class Language
{
	var $_browserLanguages;
	var $_catalogLanguages;
	var $_language;
	var $_languages = array(
		'ar' => 'ar([-_][[:alpha:]]{2})?|arabic',
		'bg' => 'bg|bulgarian',
		'br' => 'pt[-_]br|brazilian portuguese',
		'ca' => 'ca|catalan',
		'cs' => 'cs|czech',
		'da' => 'da|danish',
		'de' => 'de([-_][[:alpha:]]{2})?|german',
		'el' => 'el|greek',
		'en' => 'en([-_][[:alpha:]]{2})?|english',
		'es' => 'es([-_][[:alpha:]]{2})?|spanish',
		'et' => 'et|estonian',
		'fi' => 'fi|finnish',
		'fr' => 'fr([-_][[:alpha:]]{2})?|french',
		'gl' => 'gl|galician',
		'he' => 'he|hebrew',
		'hu' => 'hu|hungarian',
		'id' => 'id|indonesian',
		'it' => 'it|italian',
		'ja' => 'ja|japanese',
		'ko' => 'ko|korean',
		'ka' => 'ka|georgian',
		'lt' => 'lt|lithuanian',
		'lv' => 'lv|latvian',
		'nl' => 'nl([-_][[:alpha:]]{2})?|dutch',
		'no' => 'no|norwegian',
		'pl' => 'pl|polish',
		'pt' => 'pt([-_][[:alpha:]]{2})?|portuguese',
		'ro' => 'ro|romanian',
		'ru' => 'ru|russian',
		'sk' => 'sk|slovak',
		'sr' => 'sr|serbian',
		'sv' => 'sv|swedish',
		'th' => 'th|thai',
		'tr' => 'tr|turkish',
		'uk' => 'uk|ukrainian',
		'tw' => 'zh[-_]tw|chinese traditional',
		'zh' => 'zh|chinese simplified'
	);
	
	/**
	 * Constructor function
	 *
	 * @access public
	 */
	
	function Language()
	{
		global $session;
		
		// set the language
		// TODO: Move all languages to 2 char iso codes
		if (!$session->isRegistered('language'))
		{
			$session->register('language');
			$session->register('languages_id');
			// Next line ... ?
			$this->getBrowserLanguage();
			$language = $lng->language['directory'];
			$languages_id = $lng->language['id'];
		}
		
		$this->_catalogLanguages = array();
		$languages_query = tep_db_query('select languages_id, name, code, image, directory from '.TABLE_LANGUAGES.' order by sort_order');
		
		while ($languages = tep_db_fetch_array($languages_query))
		{
			$this->_catalogLanguages[$languages['code']] = array(
				'id' => $languages['languages_id'],
				'name' => $languages['name'],
				'image' => $languages['image'],
				'directory' => $languages['directory']
			);
		}
		
		$this->_browserLanguages = '';
		$this->_language = '';
		$this->setLanguage($lng);
	}
	
	/**
	 * Get the browser's accepted languages
	 *
	 * @access public
	 */
	
	function getBrowserLanguage()
	{
		$this->_browserLanguages = explode(',', getenv('HTTP_ACCEPT_LANGUAGE'));
		
		for ($i=0, $n=sizeof($this->_browserLanguages); $i<$n; $i++)
		{
			reset($this->_languages);
			
			while (list($key, $value) = each($this->_languages))
			{
				if (eregi('^(' . $value . ')(;q=[0-9]\\.[0-9])?$', $this->_browserLanguages[$i]) && isset($this->_catalogLanguages[$key]))
				{
					$this->_language = $this->_catalogLanguages[$key];
					break 2;
				}
			}
		}
	}
	
	/**
	 * Get the current language direction
	 *
	 * @todo Make this function work!
	 *
	 * @access public
	 * @return string The current language direction ('ltr' or 'rtl')
	 */
	
	function getDirection()
	{
		return 'ltr';
	}
	
	/**
	 * Get the current language
	 *
	 * @todo Make this function work!
	 *
	 * @access public
	 * @return string The current language code
	 */
	
	function getLanguage()
	{
		return 'en';
	}
	
	/**
	 * Get the current language id
	 *
	 * @todo Make this function work!
	 *
	 * @access public
	 * @return integer The current language id
	 */
	
	function getLanguageId()
	{
		return 1;
	}
	
	/**
	 * Set the current language
	 *
	 * @access public
	 * @param string $language Language code to use
	 */
	
	function setLanguage($language)
	{
		if ( (tep_not_null($language)) && (isset($this->_catalogLanguages[$language])) )
		{
			$this->_language = $this->_catalogLanguages[$language];
		}
		
		else
		{
			$this->_language = $this->_catalogLanguages[DEFAULT_LANGUAGE];
		}
	}
}

?>
